home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1320 / 1320.xpi / chrome / gmanager.jar / content / overlayToolbarMove.js < prev    next >
Text File  |  2010-01-22  |  12KB  |  387 lines

  1. // Gmail Manager
  2. // By Todd Long <longfocus@gmail.com>
  3. // http://www.longfocus.com/firefox/gmanager/
  4.  
  5. var gmanager_ToolbarMove = new function()
  6. {
  7.   this.TOOLBAR_FLAVOUR = "text/gmanager-toolbar-panel";
  8.   this.DRAGDROP_ACTION = Components.interfaces.nsIDragService.DRAGDROP_ACTION_MOVE;
  9.   
  10.   this.isActive = function()
  11.   {
  12.     return this._isActive;
  13.   }
  14.   
  15.   this.initDrag = function(aEvent)
  16.   {
  17.     this._toolbars = gmanager_Utils.getToolbars();
  18.     this._dragOverItem = null;
  19.     
  20.     if (this._toolbars.length > 0)
  21.     {
  22.       var splitter = document.getElementById("urlbar-search-splitter");
  23.       if (splitter)
  24.         splitter.parentNode.removeChild(splitter);
  25.       
  26.       this._isActive = true;
  27.       this._toggleMenuBar(true);
  28.       this._addToolbarListeners();
  29.       this._wrapToolbarItems();
  30.       
  31.       nsDragAndDrop.startDrag(aEvent, gmanager_ToolbarMove);
  32.       
  33.       // TODO: Fix the drag-and-drop service invocation
  34.       
  35.       // Borrowed from nsDragAndDrop.js prior to 3.5.*
  36.       try {
  37.         // Use the drag service already available
  38.         var dragService = nsDragAndDrop.mDragService;
  39.         var dataTransfer = aEvent.dataTransfer;
  40.         var transArray = Components.classes["@mozilla.org/supports-array;1"].createInstance(Components.interfaces.nsISupportsArray);
  41.         
  42.         // Setup the transfer data
  43.         var data = dataTransfer.getData(this.TOOLBAR_FLAVOUR);
  44.         var transferData = new TransferData();
  45.         transferData.addDataForFlavour(this.TOOLBAR_FLAVOUR, data);
  46.         
  47.         // Since we are only moving one item
  48.         var trans = nsTransferable.set(transferData);
  49.         transArray.AppendElement(trans.QueryInterface(Components.interfaces.nsISupports));
  50.         
  51.         // Invoke the modal drag session using an image
  52.         dragService.invokeDragSessionWithImage(aEvent.target, transArray,
  53.                                                null, this.DRAGDROP_ACTION,
  54.                                                null, 0, 0, aEvent, dataTransfer);
  55.       } catch(e) {
  56.         // this could be because the user pressed escape to
  57.         // cancel the drag. even if it's not, there's not much
  58.         // we can do, so be silent.
  59.       }
  60.       
  61.       this._isActive = false;
  62.       this._toggleMenuBar(false);
  63.       this._removeToolbarListeners();
  64.       this._unwrapToolbarItems();
  65.       
  66.       if (typeof UpdateUrlbarSearchSplitterState == "function")
  67.         UpdateUrlbarSearchSplitterState();
  68.     }
  69.   }
  70.   
  71.   this._toggleMenuBar = function(aBool)
  72.   {
  73.     var menubar = document.getElementById("main-menubar");
  74.     if (menubar)
  75.     {
  76.       for (var i = 0; i < menubar.childNodes.length; ++i)
  77.         menubar.childNodes[i].setAttribute("disabled", aBool);
  78.     }
  79.   }
  80.   
  81.   this._addToolbarListeners = function()
  82.   {
  83.     for (var i = 0; i < this._toolbars.length; i++)
  84.     {
  85.       this._toolbars[i].addEventListener("dragover", this._onToolbarDragOver, false);
  86.       this._toolbars[i].addEventListener("dragdrop", this._onToolbarDragDrop, false);
  87.       this._toolbars[i].addEventListener("dragexit", this._onToolbarDragExit, false);
  88.     }
  89.   }
  90.   
  91.   this._removeToolbarListeners = function()
  92.   {
  93.     for (var i = 0; i < this._toolbars.length; i++)
  94.     {
  95.       this._toolbars[i].removeEventListener("dragover", this._onToolbarDragOver, false);
  96.       this._toolbars[i].removeEventListener("dragdrop", this._onToolbarDragDrop, false);
  97.       this._toolbars[i].removeEventListener("dragexit", this._onToolbarDragExit, false);
  98.     }
  99.   }
  100.   
  101.   this._isCustomizableToolbar = function(aToolbar)
  102.   {
  103.     if (aToolbar)
  104.       return (aToolbar.localName == "toolbar" || aToolbar.localName == "statusbar");
  105.     return false;
  106.   }
  107.   
  108.   this._isToolbarItem = function(aItem)
  109.   {
  110.     if (aItem)
  111.       return aItem.localName == "toolbarbutton" ||
  112.              aItem.localName == "toolbaritem" ||
  113.              aItem.localName == "toolbarseparator" ||
  114.              aItem.localName == "toolbarspacer" ||
  115.              aItem.localName == "toolbarspring" ||
  116.              aItem.localName == "statusbarpanel";
  117.     return false;
  118.   }
  119.   
  120.   this._wrapToolbarItem = function(aItem)
  121.   {
  122.     var wrapper = this._createWrapper(aItem.id);
  123.     
  124.     wrapper.flex = aItem.flex;
  125.     
  126.     if (aItem.parentNode)
  127.       aItem.parentNode.removeChild(aItem);
  128.     
  129.     wrapper.appendChild(aItem);
  130.     
  131.     return wrapper;
  132.   }
  133.   
  134.   this._createWrapper = function(aId)
  135.   {
  136.     var wrapper = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "toolbarpaletteitem");
  137.     wrapper.id = "wrapper-" + aId;  
  138.     return wrapper;
  139.   }
  140.   
  141.   this._cleanupItemForToolbar = function(aItem, aWrapper)
  142.   {
  143.     this._setWrapperType(aItem, aWrapper);
  144.     aWrapper.setAttribute("place", "toolbar");
  145.     
  146.     if (aItem.hasAttribute("command"))
  147.     {
  148.       aWrapper.setAttribute("itemcommand", aItem.getAttribute("command"));
  149.       aItem.removeAttribute("command");
  150.     }
  151.     
  152.     // Check if the item is disabled
  153.     if (aItem.disabled)
  154.     {
  155.       aWrapper.setAttribute("itemdisabled", "true");
  156.       aItem.setAttribute("disabled", "false");
  157.     }
  158.     
  159.     // TODO: Show collapsed and disabled elements
  160.     
  161. //    // Check if the item is collapsed
  162. //    if (aItem.collapsed)
  163. //    {
  164. //      aWrapper.setAttribute("itemcollapsed", "true");
  165. //      aItem.setAttribute("collapsed", "false");
  166. //    }
  167. //    
  168. //    // Check if the item is hidden
  169. //    if (aItem.hidden)
  170. //    {
  171. //      aWrapper.setAttribute("itemhidden", "true");
  172. //      aItem.setAttribute("hidden", "false");
  173. //    }
  174.   }
  175.   
  176.   this._setWrapperType = function(aItem, aWrapper)
  177.   {
  178.     if (aItem.localName == "toolbarseparator")
  179.       aWrapper.setAttribute("type", "separator");
  180.     else if (aItem.localName == "toolbarspring")
  181.       aWrapper.setAttribute("type", "spring");
  182.     else if (aItem.localName == "toolbarspacer")
  183.       aWrapper.setAttribute("type", "spacer");
  184.     else if (aItem.localName == "toolbaritem" && aItem.firstChild)
  185.       aWrapper.setAttribute("type", aItem.firstChild.localName);
  186.   }
  187.   
  188.   this._wrapToolbarItems = function()
  189.   {
  190.     for (var i = 0; i < this._toolbars.length; i++)
  191.     {
  192.       var toolbar = this._toolbars[i];
  193.       
  194.       if (this._isCustomizableToolbar(toolbar))
  195.       {
  196.         for (var k = 0; k < toolbar.childNodes.length; k++)
  197.         {
  198.           var item = toolbar.childNodes[k];
  199.           
  200.           if (this._isToolbarItem(item))
  201.           {
  202.             var nextSibling = item.nextSibling;
  203.             var wrapper = this._wrapToolbarItem(item);
  204.             
  205.             if (nextSibling)
  206.               toolbar.insertBefore(wrapper, nextSibling);
  207.             else
  208.               toolbar.appendChild(wrapper);
  209.             
  210.             this._cleanupItemForToolbar(item, wrapper);
  211.           }
  212.         }
  213.       }
  214.     }
  215.   }
  216.   
  217.   this._unwrapToolbarItems = function()
  218.   {
  219.     for (var i = 0; i < this._toolbars.length; i++)
  220.     {
  221.       var paletteItems = this._toolbars[i].getElementsByTagName("toolbarpaletteitem");
  222.       
  223.       while (paletteItems.length > 0)
  224.       {
  225.         var paletteItem = paletteItems.item(0);
  226.         var toolbarItem = paletteItem.firstChild;
  227.         
  228.         for (var k = 0; k < paletteItem.attributes.length; k++)
  229.         {
  230.           var attributeName = paletteItem.attributes.item(k).nodeName;
  231.           var nameMatch = attributeName.match("^item(.+)$");
  232.           
  233.           if (nameMatch != null)
  234.             toolbarItem.setAttribute(nameMatch[1], paletteItem.getAttribute(attributeName));
  235.         }
  236.         
  237.         paletteItem.parentNode.replaceChild(toolbarItem, paletteItem);
  238.       }
  239.     }
  240.   }
  241.   
  242.   this._onToolbarDragOver = function(aEvent)
  243.   {
  244.     nsDragAndDrop.dragOver(aEvent, gmanager_ToolbarMove);
  245.   }
  246.   
  247.   this._onToolbarDragDrop = function(aEvent)
  248.   {
  249.     nsDragAndDrop.drop(aEvent, gmanager_ToolbarMove);
  250.   }
  251.   
  252.   this._onToolbarDragExit = function(aEvent)
  253.   {
  254.     if (this._dragOverItem)
  255.       this._setDragActive(this._dragOverItem, false);
  256.   }
  257.   
  258.   this._setDragActive = function(aItem, aBool)
  259.   {
  260.     var node = aItem;
  261.     
  262.     if (aItem && this._isCustomizableToolbar(aItem.localName))
  263.       node = aItem.lastChild;
  264.     
  265.     if (node)
  266.     {
  267.       if (aBool)
  268.       {
  269.         var direction = window.getComputedStyle(aItem, null).direction;
  270.         var value = (direction == "ltr" ? "left" : "right");
  271.         
  272.         node.setAttribute("dragover", value);
  273.       }
  274.       else
  275.         node.removeAttribute("dragover");
  276.     }
  277.   }
  278.   
  279.   this.onDragStart = function(aEvent, aXferData, aDragAction)
  280.   {
  281.     var item = aEvent.target;
  282.     while (item && item.localName != "toolbarpaletteitem")
  283.       item = item.parentNode;
  284.     
  285.     item.setAttribute("dragactive", "true");
  286.     
  287.     aXferData.data = new TransferData();
  288.     aXferData.data.addDataForFlavour(this.TOOLBAR_FLAVOUR, item.firstChild.id);
  289.     
  290.     aDragAction.action = this.DRAGDROP_ACTION;
  291.   }
  292.   
  293.   this.onDragOver = function(aEvent, aFlavour, aDragSession)
  294.   {
  295.     var toolbar = aEvent.target;
  296.     var dropTarget = aEvent.target;
  297.     
  298.     while (!this._isCustomizableToolbar(toolbar))
  299.     {
  300.       dropTarget = toolbar;
  301.       toolbar = toolbar.parentNode;
  302.     }
  303.     
  304.     var previousDragItem = this._dragOverItem;
  305.     
  306.     if (this._isCustomizableToolbar(dropTarget))
  307.       this._dragOverItem = dropTarget;
  308.     else
  309.     {
  310.       var direction = window.getComputedStyle(dropTarget.parentNode, null).direction;
  311.       var dropTargetCenter = (dropTarget.boxObject.x + (dropTarget.boxObject.width / 2));
  312.       var dragAfter = (direction == "ltr" ? aEvent.clientX > dropTargetCenter : aEvent.clientX < dropTargetCenter);
  313.       
  314.       if (dragAfter)
  315.         this._dragOverItem = (dropTarget.nextSibling ? dropTarget.nextSibling : toolbar);
  316.       else
  317.         this._dragOverItem = dropTarget;
  318.     }
  319.     
  320.     if (previousDragItem && previousDragItem != this._dragOverItem)
  321.       this._setDragActive(previousDragItem, false);
  322.     
  323.     this._setDragActive(this._dragOverItem, true);
  324.     
  325.     aDragSession.canDrop = true;
  326.   }
  327.   
  328.   this.onDrop = function(aEvent, aXferData, aDragSession)
  329.   {
  330.     if (!this._dragOverItem)
  331.       return;
  332.     
  333.     this._setDragActive(this._dragOverItem, false);
  334.     
  335.     var draggedItemId = aXferData.data;
  336.     if (draggedItemId == this._dragOverItem.id)
  337.       return;
  338.     
  339.     var toolbar = aEvent.target;
  340.     while (!this._isCustomizableToolbar(toolbar))
  341.       toolbar = toolbar.parentNode;
  342.     
  343.     var wrapper = document.getElementById("wrapper-" + draggedItemId);
  344.     if (wrapper == this._dragOverItem)
  345.       return;
  346.     
  347.     // Remove the item from its current toolbar
  348.     wrapper.parentNode.removeChild(wrapper);
  349.     
  350.     // Insert the item in the new toolbar
  351.     if (toolbar != this._dragOverItem)
  352.       toolbar.insertBefore(wrapper, this._dragOverItem);
  353.     else
  354.       toolbar.appendChild(wrapper);
  355.     
  356.     var toolbarPanel = wrapper.firstChild;
  357.     if (gmanager_Utils.isAccountToolbar(toolbarPanel))
  358.     {
  359.       var account = toolbarPanel.account;
  360.       if (account)
  361.       {
  362.         var specificPosition = -1;
  363.         var toolbarChildren = toolbar.childNodes;
  364.         for (var i = 0; i < toolbarChildren.length; i++)
  365.         {
  366.           var toolbarNode = toolbarChildren.item(i);
  367.           if (wrapper.id == toolbarNode.id)
  368.             specificPosition = i;
  369.         }
  370.         
  371.         account.setCharPref("toolbar-toolbar-id", toolbar.id);
  372.         account.setCharPref("toolbar-placement", "specific-position");
  373.         account.setIntPref("toolbar-specific-position", specificPosition);
  374.         
  375.         var manager = Components.classes["@longfocus.com/gmanager/manager;1"].getService(Components.interfaces.gmIManager);
  376.         manager.save();
  377.       }
  378.     }
  379.   }
  380.   
  381.   this.getSupportedFlavours = function()
  382.   {
  383.     var flavours = new FlavourSet();
  384.     flavours.appendFlavour(this.TOOLBAR_FLAVOUR);
  385.     return flavours;
  386.   }
  387. }